# 字典
alien_0 = {"color":"green", "points":5}
print(alien_0)
print(alien_0["color"])
print(alien_0["points"])

# 使用字典
new_points = alien_0["points"]
print(f"You just earned {new_points} points!")

# 添加键值对
alien_0 = {"color":"green", "points":5}
print(alien_0)
alien_0["x_position"] = 0
alien_0["y_position"] = 25
alien_0["color"] = "blue"
print(alien_0)

# 创建空字典
alien_0 = {}
alien_0["color"] = "green"
alien_0["points"] = 5
print(alien_0)

# 修改字典的值
alien_0 = {"color":"green", "points":5}
print(alien_0)
alien_0["color"] = "blue"
print(alien_0)


# 例子
alien_0 = {"x_position": 0 ,"y_position" : 25, "speed" : "medium"}
print(f"Original position: {alien_0['x_position']}")
if alien_0["speed"] == "slow":
    x_increment = 1
elif alien_0["speed"] == "medium":
    x_increment = 2
else :
    x_increment = 3
alien_0["x_position"] = alien_0["x_position"] + x_increment
print(f"New position: {alien_0['x_position']}")


# 删除键值对
alien_0 = {"color":"green", "points":5}
print(alien_0)

del alien_0["points"]
print(alien_0)

# 例子
favorite_langeage = {
    "jen" : "python",
    "sarah" : "c",
    "edward" : "rust",
    "phil" : "python"
}
print(favorite_langeage)
print(favorite_langeage["sarah"])
print(len(favorite_langeage))

# 使用get()访问值
alien_0 = {"color" : "green", "speed" : "slow"}
#print(alien_0["points"]) ##将会报错
point_value = alien_0.get("points","no point value assigned.")
print(point_value)

# 遍历字典
user_0 = {
    "username" : "efermi",
    "first" : "enrico",
    "last" : "fermi"
}
for key,value in user_0.items():
    print(f"\nKey:{key}")
    print(f"Value:{value}")
print(user_0.items()) # 该方法返回一个键值对列表


# 遍历所有键
favorite_langeage = {
    "jen": "python",
    "sarah" : "c",
    "edward" : "rust",
    "phil" : "python"
}
for name in favorite_langeage.keys():
    print(name.title())

#遍历例子
favorite_langeage = {
    "jen": "python",
    "sarah" : "c",
    "edward" : "rust",
    "phil" : "python"
}
friend = ["phil","sarah"]
for name in favorite_langeage.keys():
    print(f"Hi {name}")
    if name in friend:
        language = favorite_langeage[name].title()
        print(f"\t {name.title()}, I see you love {language}!")

# 按照特定顺序遍历
favorite_langeage = {
    "jen": "python",
    "sarah" : "c",
    "edward" : "rust",
    "phil" : "python"
}
for name in sorted(favorite_langeage.keys()):
    print(f"{name.title()}, thank you for taking the poll")
print(favorite_langeage)

# 遍历字典所有值 引入集合概念
favorite_langeage = {
    "jen": "python",
    "sarah" : "c",
    "edward" : "rust",
    "phil" : "python"
}
print(favorite_langeage)
values = set()
for value in favorite_langeage.values():
    print(value.title())
    values.add(value)
    print(values)
# 集合
language = {"python","c","python","rust"}
print(language)

# 嵌套
alien_0 = {"color":"green","points":5}
alien_1 = {"color":"yellow","points":10}
alien_2 = {"color":"red","points":15}

aliens = [alien_0,alien_1,alien_2]
for alien in aliens:
    print(alien)

# 例子
aliens = []
for alien_number in range(30):
    new_alien = {"color":"green","point":5,"speed":"slow"}
    aliens.append(new_alien)
for alien in aliens[:3]:
    if alien["color"] == "green":
        alien["color"] = "yellow"
        alien["speed"] = "medium"
        alien["point"] = 10
    elif alien["color"] == "yellow":
        alien["color"] = "red"
        alien["speed"] = "fast"
        alien["point"] = 15
for alien in aliens:
    print(alien)
print(f"total number of aliens: {len(aliens)}")


# 字典中储存列表
pizza = {
    "crust": "thick",
    "toppings" : ["mushrooms","extra cheese"],
}
print(f"You ordered a {pizza['crust']}-crust pizza "
      "with th following topppings:")
for topping in pizza["toppings"]:
    print(f"\t{topping}")


favorite_langeage = {
    "jen": ["python","rust"],
    "sarah" : ["c"],
    "edward" : ["rust","go"],
    "phil" : ["python","haskell"]
}
for name,languages in favorite_langeage.items():
    print(f"\n{name.title()}'s favorite language are:")
    for language in languages:
        print(f"\t{language.title()}")


# 字典中存储字典
users = {
    "aeinstein" : {
        "first":"albert",
        "last":"einstein",
        "location":"princeton"
    },
    "mcurie" : {
        "first":"marie",
        "last":"curie",
        "location":"paris"
    }
}
for username,user_info in users.items():
    print(f"user name:{username}")
    full_name = f"{user_info['first']} {user_info['last']}"
    location = user_info["location"]
    print(f"\tFullname:{full_name}")
    print(f"\tLocation:{location}")